﻿using System.Configuration;
using System.Data.Services.Client;
using System.Web.Security;
using AzureQueue;
using AzureTableStorage;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace WebPages.Bll
{
    public class InitApplication
    {
        bool AdminExists(string adminName)
        {
            try
            {
                if (Membership.GetUser(adminName) == null) return false;
            }
            catch (DataServiceQueryException)
            {
                return false;
            }

            return true;
        }

        void CreateAdminUserIfNotExists()
        {
            var adminName = ConfigurationManager.AppSettings["AdminName"];

            if (AdminExists(adminName)) return;

            MembershipCreateStatus status;
            var user = Membership.CreateUser(
                adminName,
                ConfigurationManager.AppSettings["AdminPassword"],
                ConfigurationManager.AppSettings["AdminEmail"],
                ConfigurationManager.AppSettings["AdminQuestion"],
                ConfigurationManager.AppSettings["AdminAnswer"], true, null, out status);

            if (status != MembershipCreateStatus.Success || user == null) return;

            user.IsApproved = true;
            Membership.UpdateUser(user);
        }

        bool AdminsRoleExists(string adminsRoleName)
        {
            try
            {
                return Roles.RoleExists(adminsRoleName);
            }
            catch (DataServiceQueryException)
            {
                return false;
            }
        }

        void CreateAdminsRoleIfNotExists()
        {
            var adminsRoleName = ConfigurationManager.AppSettings["AdminRoleName"];
            if (AdminsRoleExists(adminsRoleName)) return;

            Roles.CreateRole(adminsRoleName);
        }

        void CreateTablesIfNotExists()
        {
            new AzureTableStorageDataSource().CreateTablesIfTheyDontExists();
        }

        void CreateQueuesIfNotExists()
        {
            new AzureQueueDataSource().CreateQueuesIfTheyDontExists();
        }

        void SetBlobPermissions(CloudBlobContainer blobContainer)
        {
            // Setup the permissions on the container to be public
            var permissions = new BlobContainerPermissions
            {
                PublicAccess = BlobContainerPublicAccessType.Container
            };

            blobContainer.SetPermissions(permissions);
        }

        void CreateBlobIfNotExists()
        {
            // Setup the connection to Windows Azure Storage
            var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            var blobClient = storageAccount.CreateCloudBlobClient();

            // Get and create the container
            var blobContainer = blobClient.GetContainerReference("publicfiles");
            if (blobContainer.CreateIfNotExist()) SetBlobPermissions(blobContainer);
        }

        public InitApplication()
        {
            CreateTablesIfNotExists();
            CreateQueuesIfNotExists();
            CreateAdminUserIfNotExists();
            CreateAdminsRoleIfNotExists();  
            CreateBlobIfNotExists();
        }

    }
}